home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / cacheviewer-0.4.7.1-fx.xpi / chrome / cacheviewer.jar / content / cacheviewer / cacheviewer.js < prev    next >
Text File  |  2008-04-13  |  21KB  |  606 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents (ry
  5.  *
  6.  * ***** END LICENSE BLOCK *****
  7.  */
  8.  
  9. const Cc = Components.classes;
  10. const Ci = Components.interfaces;
  11. const Cr = Components.results;
  12.  
  13. var CacheViewer = {
  14.     
  15.     // ***** Members *****
  16.     _tree: null,
  17.     _bundle: null,
  18.     
  19.     _entries: null,
  20.     _rdf: null,
  21.     _root: null,
  22.     _metaData: "",
  23.     _isOffline: false,
  24.     _visitAll: true,
  25.     _isLoading: false,
  26.     
  27.     get _cacheService() {
  28.         if (!this.__cacheService) {
  29.             this.__cacheService = Cc["@mozilla.org/network/cache-service;1"]
  30.                             .getService(Ci.nsICacheService);
  31.         }
  32.         return this.__cacheService;
  33.         /*
  34.         return Cc["@mozilla.org/network/cache-service;1"]
  35.                 .getService(Ci.nsICacheService);
  36.         */
  37.     },
  38.     __cacheService: null,
  39.     
  40.     get _dateService() {
  41.         if (!this.__dateService) {
  42.             this.__dateService = Cc["@mozilla.org/intl/scriptabledateformat;1"]
  43.                             .getService(Ci.nsIScriptableDateFormat);
  44.         }
  45.         return this.__dateService;
  46.     },
  47.     __dateService: null,
  48.     
  49.     // ***** CacheViewer *****
  50.     init: function CV_init() {
  51.         var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  52.         this._isOffline = ioService.offline;
  53.         
  54.         this._tree = document.getElementById("cacheTree");
  55.         this._bundle = document.getElementById("strings");
  56.         
  57.         this._entries = new Array();
  58.         this._rdf = new RDF();
  59.         this._root = this._rdf.makeSeqContainer(this._rdf.RDF_ITEM_ROOT);
  60.         
  61.         this._tree.database.AddDataSource(this._rdf.datasource);
  62.         this._tree.setAttribute("ref", this._rdf.RDF_ITEM_ROOT);
  63.         
  64.         this._visitAll = true;
  65.         this._cacheService.visitEntries(this);
  66.         
  67.         var it = Iterator(this._entries);
  68.         
  69.         var timer = Components.classes["@mozilla.org/timer;1"]
  70.                     .createInstance(Components.interfaces.nsITimer);
  71.         
  72.         var self = this;
  73.         
  74.         function timerCallback() {}
  75.         timerCallback.prototype = {
  76.             observe: function(aTimer, aTopic, aData) {
  77.                 try { var [index, value] = it.next(); }
  78.                 catch(e) { self._toggleButton(self._isLoading = false); return; }
  79.                 
  80.                 if (!self._rdf) return;
  81.                 
  82.                 //self._rdf.datasource.beginUpdateBatch();
  83.                 var resource = self._rdf.appendResource(index, self._root);
  84.                 self._rdf.setLiteralProperty(resource, self._rdf.NS_CACHEVIEWER+"key", value[0]);
  85.                 self._rdf.setIntProperty(resource, self._rdf.NS_CACHEVIEWER+"size", value[1]);
  86.                 self._rdf.setLiteralProperty(resource, self._rdf.NS_CACHEVIEWER+"dev", value[2]);
  87.                 self._rdf.setLiteralProperty(resource, self._rdf.NS_CACHEVIEWER+"clnt", value[3]);
  88.                 self._rdf.setLiteralProperty(resource, self._rdf.NS_CACHEVIEWER+"strm", value[4]);
  89.                 self._rdf.setDateProperty(resource, self._rdf.NS_CACHEVIEWER+"mod", value[5]*1000000);
  90.                 self._rdf.setDateProperty(resource, self._rdf.NS_CACHEVIEWER+"fet", value[6]*1000000);
  91.                 self._rdf.setDateProperty(resource, self._rdf.NS_CACHEVIEWER+"exp", value[7]*1000000);
  92.                 self._rdf.setIntProperty(resource, self._rdf.NS_CACHEVIEWER+"cnt", value[8]);
  93.                 self._rdf.setLiteralProperty(resource, self._rdf.NS_CACHEVIEWER+"type", self._getMimeType(value[0], value[3], value[4]));
  94.                 //self._getMimeType(resource, value[0], value[3], value[4]);
  95.                 //self._rdf.datasource.endUpdateBatch();
  96.                 timer.init(new timerCallback(), 0, timer.TYPE_ONE_SHOT);
  97.             }
  98.         };
  99.         
  100.         this._toggleButton(this._isLoading = true);
  101.         timer.init(new timerCallback(), 0, timer.TYPE_ONE_SHOT);
  102.     },
  103.     
  104.     finish: function CV_finish() {
  105.         var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  106.         ioService.offline = this._isOffline;
  107.         
  108.         this._root = null;
  109.         this._rdf = null;
  110.         this._entries = null;
  111.         this._bundle = null;
  112.         this._tree = null;
  113.     },
  114.     
  115.     openCache: function CV_openCache() {
  116.         var resource = this._getResourceAtCurrentIndex();
  117.         if (!resource) return;
  118.         
  119.         var key = this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"key");
  120.         var client = this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"clnt");
  121.         var stream = this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"strm") == "true" ? "1" :"0";
  122.         
  123.         this._getBrowser().selectedTab = this._getBrowser().addTab(key);
  124.     },
  125.     
  126.     deleteCache: function CV_deleteCache() {
  127.         var resource = this._getResourceAtCurrentIndex();
  128.         if (!resource) return;
  129.         
  130.         var key = this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"key");
  131.         var client = this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"clnt");
  132.         var stream = this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"strm") == "true" ? true : false;
  133.         var entry = this._openCacheEntry(key, client, stream);
  134.         if (!entry) return;
  135.         
  136.         entry.doom();
  137.         entry.close();
  138.         
  139.         this._updateUI();
  140.     },
  141.     
  142.     reloadCache: function CV_reloadCache() {
  143.         if (this._isLoading) return;
  144.         
  145.         var image = document.getElementById("previewImage");
  146.         image.src = "";
  147.         document.getElementById("cacheInfo").value = "";
  148.         
  149.         this._tree.database.RemoveDataSource(this._rdf.datasource);
  150.         this._tree.builder.rebuild();
  151.         
  152.         this._rdf = null;
  153.         this.__cacheService = null;
  154.         
  155.         this.init();
  156.     },
  157.     
  158.     saveCache: function CV_saveCache() {
  159.         var selection = [];
  160.         var rangeCount = this._tree.view.selection.getRangeCount();
  161.         for (var i=0; i<rangeCount; ++i) {
  162.             var rangeMin = {};
  163.             var rangeMax = {};
  164.             this._tree.view.selection.getRangeAt(i, rangeMin, rangeMax);
  165.             for (var j=rangeMin.value; j<=rangeMax.value; ++j) {
  166.                 selection.push(this._tree.view.getResourceAtIndex(j));
  167.             }
  168.         }
  169.         
  170.         if (selection.length == 1) {
  171.             var key = this._rdf.getLiteralProperty(selection[0], this._rdf.NS_CACHEVIEWER+"key");
  172.             saveURL(key, null, null, false);
  173.             return;
  174.         }
  175.         
  176.         var pref = Cc["@mozilla.org/preferences-service;1"]
  177.                     .getService(Ci.nsIPrefService)
  178.                     .getBranch("extensions.cacheviewer.");
  179.         var lastFolderPath = pref.getComplexValue("folder", Ci.nsISupportsString).data;
  180.         var lastFolder = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile)
  181.         if (lastFolderPath)
  182.             lastFolder.initWithPath(lastFolderPath);
  183.         
  184.         var fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
  185.         fp.init(window, null, Ci.nsIFilePicker.modeGetFolder);
  186.         fp.displayDirectory = lastFolder;
  187.         var res = fp.show();
  188.         if (res != Ci.nsIFilePicker.returnOK)
  189.             return;
  190.         
  191.         var str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
  192.         str.data = fp.file.path;
  193.         pref.setComplexValue("folder", Ci.nsISupportsString, str);
  194.         
  195.         var folder = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  196.         folder.initWithPath(fp.file.path);
  197.         
  198.         var URIFix = Cc["@mozilla.org/docshell/urifixup;1"].getService(Ci.nsIURIFixup);
  199.         
  200.         for (i=0; i<selection.length; i++) {
  201.             var key = this._rdf.getLiteralProperty(selection[i], this._rdf.NS_CACHEVIEWER+"key");
  202.             var client = this._rdf.getLiteralProperty(selection[i], this._rdf.NS_CACHEVIEWER+"clnt");
  203.             var stream = this._rdf.getLiteralProperty(selection[i], this._rdf.NS_CACHEVIEWER+"strm") == "true" ? true : false;
  204.             var device = this._rdf.getLiteralProperty(selection[i], this._rdf.NS_CACHEVIEWER+"dev");
  205.             
  206.             var descriptor = this._openCacheEntry(key, client, stream);
  207.             if (!descriptor) {
  208.                 continue;
  209.             }
  210.             
  211.             var URI = URIFix.createFixupURI(key, 0);
  212.             var fileInfo = new FileInfo();
  213.             initFileInfo(fileInfo, URI.spec, null, null, null, null);
  214.             var file = folder.clone();
  215.             file.append(fileInfo.fileBaseName+"."+fileInfo.fileExt);
  216.             file.createUnique(Ci.nsILocalFile.NORMAL_FILE_TYPE, 0666);
  217.             
  218.             // If memory cache, use "internalSave".
  219.             // (See chrome://chrome://global/content/contentAreaUtils.js)
  220.             if (device == "memory") {
  221.                 var auto = new AutoChosen(file, makeURI(key));
  222.                 internalSave(key, null, null, null,
  223.                              null, false, null,
  224.                              auto, null, true);
  225.                 continue;
  226.             }
  227.             
  228.             // Check the encoding.
  229.             var metaData = "";
  230.             var encode = "";
  231.             var visitor = {
  232.                 visitMetaDataElement: function(aKey, aValue) {
  233.                     metaData += aKey + ": " + aValue + "\n";
  234.                     return true;
  235.                 }
  236.             };
  237.             descriptor.visitMetaData(visitor);
  238.             if (metaData.match(/Content-Encoding: (.+)$/m)) {
  239.                 encode = RegExp.$1;
  240.             }
  241.             
  242.             try {
  243.                 var inputStream = descriptor.openInputStream(0);
  244.                 
  245.                 if (encode) {
  246.                     var converterService = Cc["@mozilla.org/streamConverters;1"]
  247.                                     .getService(Ci.nsIStreamConverterService);
  248.                     var converter = converterService.asyncConvertData(encode, "uncompressed", new StreamListener(file), null);
  249.                     converter.onStartRequest(null, null);
  250.                     converter.onDataAvailable(null, null, inputStream, 0, inputStream.available());
  251.                     converter.onStopRequest(null, null, null);
  252.                     continue;
  253.                 }
  254.                 
  255.                 var fileOutputStream = Cc["@mozilla.org/network/file-output-stream;1"]
  256.                                             .createInstance(Ci.nsIFileOutputStream);
  257.                 var binaryInputStream = Cc["@mozilla.org/binaryinputstream;1"]
  258.                                             .createInstance(Ci.nsIBinaryInputStream);
  259.                 binaryInputStream.setInputStream(inputStream);
  260.                 var content = binaryInputStream.readBytes(binaryInputStream.available());
  261.                 fileOutputStream.init(file, -1, 0666, 0);
  262.                 fileOutputStream.write(content, content.length);
  263.                 fileOutputStream.flush();
  264.             } catch(e) {
  265.                 dump(e+"\n");
  266.                 file.remove(false);
  267.             }
  268.             binaryInputStream.close();
  269.             fileOutputStream.close();
  270.         }
  271.     },
  272.     
  273.     search: function CV_search(aSearchString) {
  274.         aSearchString = aSearchString.replace(/^ +/, "");
  275.         if (aSearchString) {
  276.             this._rdf.search(aSearchString);
  277.             this._tree.ref = this._rdf.RDF_ITEM_SEARCH;
  278.         } else {
  279.             this._tree.ref = this._rdf.RDF_ITEM_ROOT;
  280.         }
  281.         document.getElementById("showall").disabled = !aSearchString;
  282.     },
  283.     
  284.     showAll: function CV_showAll() {
  285.         var textbox = document.getElementById("search");
  286.         textbox.value = "";
  287.         textbox.focus();
  288.         this.search("");
  289.     },
  290.     
  291.     onSelect: function CV_onSelect() {
  292.         if (this._tree.view.selection.count == 1) {
  293.             var timer = Components.classes["@mozilla.org/timer;1"]
  294.                         .createInstance(Components.interfaces.nsITimer);
  295.             var self = this;
  296.             function timerCallback() {}
  297.             timerCallback.prototype = {
  298.                 observe: function(aTimer, aTopic, aData) {
  299.                     self._makePreview(self._tree.view.selection.currentIndex);
  300.                 }
  301.             }
  302.             timer.init(new timerCallback(), 0, timer.TYPE_ONE_SHOT);
  303.         }
  304.     },
  305.     
  306.     onPopupShowing: function CV_onPopupShowing() {
  307.         var resource = this._getResourceAtCurrentIndex();
  308.         if (!resource) return;
  309.         
  310.         var dev = this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"dev");
  311.         var menu = document.getElementById("deleteCache");
  312.         if (dev != "disk")
  313.             menu.setAttribute("disabled", "true");
  314.         else
  315.             if (menu.hasAttribute("disabled"))
  316.                 menu.removeAttribute("disabled");
  317.     },
  318.     
  319.     resize: function CV_resize() {
  320.         var image = document.getElementById("previewImage");
  321.         if (image.hasAttribute("style")) {
  322.             image.removeAttribute("style");
  323.             return;
  324.         }
  325.         
  326.         var width = parseInt(window.getComputedStyle(image, "").width.replace("px", ""));
  327.         var height = parseInt(window.getComputedStyle(image, "").height.replace("px", ""));
  328.         
  329.         var containerWidth = parseInt(window.getComputedStyle(image.parentNode.parentNode, "").width.replace("px", ""));
  330.         var containerHeight = parseInt(window.getComputedStyle(image.parentNode.parentNode, "").height.replace("px", ""));
  331.         
  332.         if (width > containerWidth) {
  333.             var zoomX = containerWidth / width;
  334.             width = containerWidth - 2;
  335.             height = height * zoomX - 2;
  336.         }
  337.         if (height > containerHeight) {
  338.             var zoomY = containerHeight / height;
  339.             height = containerHeight - 2;
  340.             width = width * zoomY - 2;
  341.         }
  342.         image.setAttribute("style", "width:"+width+"px;"+"height:"+height+"px;");
  343.     },
  344.     
  345.     // ***** nsICacheVisitor *****
  346.     visitDevice: function CV_visitDevice(aDeviceID, aDeviceInfo) {
  347.         if (aDeviceID == "offline") return true;
  348.         
  349.         document.getElementById(aDeviceID).setAttribute("value", Math.round(aDeviceInfo.totalSize/1024000*100)/100+"/"+Math.round(aDeviceInfo.maximumSize/1024000*100)/100);
  350.         document.getElementById(aDeviceID + "Meter").setAttribute("value", Math.round(aDeviceInfo.totalSize/aDeviceInfo.maximumSize*100));
  351.         document.getElementById(aDeviceID + "Entries").setAttribute("value", aDeviceInfo.entryCount +" "+ this._bundle.getString("entries"));
  352.         return true;
  353.     },
  354.     
  355.     visitEntry: function CV_visitEntry(aDeviceID, aEntryInfo) {
  356.         if (!this._visitAll)
  357.             return false;
  358.         
  359.         if (aEntryInfo.dataSize == 0)
  360.             return true;
  361.         
  362.         if (aEntryInfo.key.indexOf("http") == 0) {
  363.             var temp = [aEntryInfo.key,
  364.                         aEntryInfo.dataSize,
  365.                         aEntryInfo.deviceID,
  366.                         aEntryInfo.clientID,
  367.                         aEntryInfo.isStreamBased(),
  368.                         aEntryInfo.lastFetched,
  369.                         aEntryInfo.lastModified,
  370.                         aEntryInfo.expirationTime,
  371.                         aEntryInfo.fetchCount];
  372.             this._entries.push(temp);
  373.         }
  374.         return true;
  375.     },
  376.     
  377.     // ***** nsICacheMetaDataVisitor *****
  378.     visitMetaDataElement: function CV_visitMetaDataElement(aKey, aValue) {
  379.         this._metaData += aKey + ": " + aValue + "\n";
  380.         return true;
  381.     },
  382.     
  383.     // ***** Helper funcitons *****
  384.     _makePreview: function CV__makePreview(aRow) {
  385.         var resource = this._tree.view.getResourceAtIndex(aRow);
  386.         
  387.         var key = this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"key");
  388.         var device =  this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"dev");
  389.         var type =  this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"type");
  390.         var client =  this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"clnt");
  391.         var stream =  this._rdf.getLiteralProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"strm") == "true" ? true : false;
  392.         
  393.         var descriptor = this._openCacheEntry(key, client, stream);
  394.         if (!descriptor) return;
  395.         
  396.         var value = this._bundle.getString("key")        + " " + descriptor.key + "\n" +
  397.                     this._bundle.getString("size")       + " " + descriptor.dataSize + " bytes\n" +
  398.                     this._bundle.getString("count")      + " " + descriptor.fetchCount + "\n" +
  399.                     this._bundle.getString("modified")   + " " + this._formatDate(descriptor.lastModified*1000000) + "\n" +
  400.                     this._bundle.getString("fetched")    + " " + this._formatDate(descriptor.lastFetched*1000000) + "\n" + 
  401.                     this._bundle.getString("expiration") + " ";
  402.                     
  403.         if (parseInt(0xFFFFFFFF) <= descriptor.expirationTime)
  404.             value += this._bundle.getString("noexpiration") + "\n";
  405.         else
  406.             value += this._formatDate(this._rdf.getDateProperty(resource.Value, this._rdf.NS_CACHEVIEWER+"exp")) + "\n";
  407.             
  408.         value += this._bundle.getString("fileondisk") + " ";
  409.         
  410.         var cacheFile;
  411.         try { cacheFile = descriptor.file; } catch(e) { cacheFile = null; }
  412.         if (cacheFile)
  413.             value += cacheFile.path + "\n\n";
  414.         else
  415.             value += this._bundle.getString("nofile") + "\n\n";
  416.         
  417.         this._metaData = "";
  418.         descriptor.visitMetaData(this);
  419.         value += this._metaData;
  420.         descriptor.close();
  421.         
  422.         document.getElementById("cacheInfo").value = value;
  423.         
  424.         // Load only from cache.
  425.         var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  426.         ioService.offline = true;
  427.         
  428.         var url = "chrome://cacheviewer/content/not_image.png";
  429.         if ((type.indexOf("image") == 0) ||
  430.             (key.match(/.*(\.png|\.gif|\.jpg|\.ico|\.bmp)$/i))) {
  431.                 url = key;
  432.         }
  433.  
  434.         var image = document.getElementById("previewImage");
  435.         image.src = url;
  436.         if (image.hasAttribute("style"))
  437.             image.removeAttribute("style");
  438.         
  439.         var self = this;
  440.         image.onload = function() {
  441.             self.resize();
  442.         }
  443.         ioService.offline = this._isOffline;
  444.     },
  445.     
  446.     _getResourceAtCurrentIndex: function CV__getResourceAtCurrentIndex() {
  447.         if (!this._tree.view.selection || this._tree.view.selection.count != 1)
  448.             return null;
  449.             
  450.         return this._tree.view.getResourceAtIndex(this._tree.view.selection.currentIndex);
  451.     },
  452.     
  453.     _getBrowser: function CV__getBrowser() {
  454.         return this._getTopWin().document.getElementById("content");
  455.     },
  456.     
  457.     _getTopWin: function CV__getTopWin() {
  458.         var windowManager = Cc["@mozilla.org/appshell/window-mediator;1"].getService();
  459.         var windowManagerInterface = windowManager.QueryInterface(Ci.nsIWindowMediator);
  460.         return windowManagerInterface.getMostRecentWindow("navigator:browser");
  461.     },
  462.     
  463.     _updateUI: function CV_updateUI() {
  464.         var currentIndex = this._tree.view.selection.currentIndex;
  465.         
  466.         this._rdf.removeResource(this._getResourceAtCurrentIndex(), this._rdf.getContainer(this._rdf.RDF_ITEM_ROOT));
  467.         if (this._tree.ref == this._rdf.RDF_ITEM_SEARCH)
  468.             this._rdf.removeResource(this._getResourceAtCurrentIndex(), this._rdf.getContainer(this._rdf.RDF_ITEM_SEARCH));
  469.         var rowCount = this._tree.view.rowCount;
  470.         
  471.         if (currentIndex == rowCount)
  472.             currentIndex--;
  473.         if (rowCount > 0)
  474.             this._tree.view.selection.select(currentIndex);
  475.         else
  476.             document.getElementById("cacheInfo").value = "";
  477.         
  478.         var cacheService = Cc["@mozilla.org/network/cache-service;1"]
  479.                         .getService(Ci.nsICacheService);
  480.         this._visitAll = false;
  481.         cacheService.visitEntries(this);
  482.     },
  483.     
  484.     _toggleButton: function CV__toggleButton(aIsLoading) {
  485.         var reload = document.getElementById("reload");
  486.         if (aIsLoading) {
  487.             if (reload.hasAttribute("enable"))
  488.                 reload.removeAttribute("enable");
  489.         } else {
  490.             reload.setAttribute("enable", "true");
  491.         }
  492.     },
  493.     
  494.     _formatDate: function CV__formatDate(aTime) {
  495.         var date = new Date(aTime/1000);
  496.         return this._dateService.FormatDateTime("",
  497.                     this._dateService.dateFormatLong,
  498.                     this._dateService.timeFormatSeconds,
  499.                     date.getFullYear(),
  500.                     date.getMonth()+1,
  501.                     date.getDate(),
  502.                     date.getHours(),
  503.                     date.getMinutes(),
  504.                     date.getSeconds());
  505.     },
  506.     /*
  507.     _getMimeType: function CV__getMimeType(aResource, aKey, aClientID, aStreamBased) {
  508.         var self = this;
  509.         function CacheListener(resource) {
  510.             this._resource = resource;
  511.         }
  512.         CacheListener.prototype = {
  513.             onCacheEntryAvailable: function(aDescriptor, aAccessGranted, aStatus) {
  514.                 var type = "-";
  515.                 if (aDescriptor && aStatus == Cr.NS_OK) {
  516.                     try {
  517.                         var header = aDescriptor.getMetaDataElement("response-head")
  518.                                     .match(/^Content-Type:\s*(.*?)\s*(?:\;|$)/mi);
  519.                     } catch(e) {
  520.                     } finally {
  521.                         aDescriptor.close();
  522.                     }
  523.                     if (header)
  524.                         type = header[1];
  525.                 }
  526.                 self._rdf.setLiteralProperty(this._resource, self._rdf.NS_CACHEVIEWER+"type", type);
  527.             }
  528.         };
  529.         try {
  530.             var session = this._cacheService.createSession(aClientID, Ci.nsICache.STORE_ANYWHERE, aStreamBased);
  531.             session.doomEntriesIfExpired = false;
  532.             session.asyncOpenCacheEntry(aKey, Ci.nsICache.ACCESS_READ, new CacheListener(aResource));
  533.         } catch(e) {
  534.             dump(e+"\n");
  535.             this._rdf.setLiteralProperty(aResource, this._rdf.NS_CACHEVIEWER+"type", "-");
  536.         }
  537.     },
  538.     */
  539.     _getMimeType: function CV__getMimeType(aKey, aClientID, aStreamBased) {
  540.         var descriptor = this._openCacheEntry(aKey, aClientID, aStreamBased);
  541.         if (!descriptor)
  542.             return "-";
  543.         
  544.         var retval = "-";
  545.         try {
  546.             var header = descriptor.getMetaDataElement("response-head")
  547.                         .match(/^Content-Type:\s*(.*?)\s*(?:\;|$)/mi);
  548.         } catch(e) {
  549.             descriptor.close();
  550.             return retval;
  551.         } 
  552.         descriptor.close();
  553.         if (header)
  554.             retval = header[1];
  555.         return retval;
  556.     },
  557.     
  558.     _openCacheEntry: function CV__openCacheEntry(aKey, aClientID, aStreamBased) {
  559.         try {
  560.             var session = this._cacheService.createSession(aClientID, Ci.nsICache.STORE_ANYWHERE, aStreamBased);
  561.             session.doomEntriesIfExpired = false;
  562.             return session.openCacheEntry(aKey, Ci.nsICache.ACCESS_READ, false);
  563.         } catch(e) {
  564.             //dump(e+"\n");
  565.             // 0x804b003d NS_ERROR_CACHE_KEY_NOT_FOUND
  566.             // 0x804b0040 NS_ERROR_CACHE_WAIT_FOR_VALIDATION 
  567.             // 0x804b0044 NS_ERROR_CACHE_IN_USE 
  568.             return null;
  569.         }
  570.     }
  571. };
  572.  
  573. // ***** StreamListener for Asynchronous Converter *****
  574. function StreamListener(aFile) {
  575.     this._file = aFile;
  576.     this._data = null;
  577. }
  578.  
  579. StreamListener.prototype = {
  580.     
  581.     onStartRequest: function(aRequest, aContext) {},
  582.     
  583.     onStopRequest: function(aRequest, aContext, aStatusCode) {
  584.         var fileOutputStream = Cc["@mozilla.org/network/file-output-stream;1"]
  585.                                             .createInstance(Ci.nsIFileOutputStream);
  586.         try {
  587.             fileOutputStream.init(this._file, -1, 0666, 0);
  588.             fileOutputStream.write(this._data, this._data.length);
  589.             fileOutputStream.flush();
  590.         } catch(e) {
  591.             dump(e+"\n");
  592.             this._file.remove(false);
  593.         } finally {
  594.             fileOutputStream.close();
  595.         }
  596.     },
  597.     
  598.     onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aCount) {
  599.         var binaryInputStream = Cc["@mozilla.org/binaryinputstream;1"]
  600.                                             .createInstance(Ci.nsIBinaryInputStream);
  601.         binaryInputStream.setInputStream(aInputStream);
  602.         this._data += binaryInputStream.readBytes(binaryInputStream.available());
  603.         binaryInputStream.close();
  604.     }
  605. };
  606.